package com.android.zhhr.data.commons;

import android.content.Context;
import android.support.annotation.Nullable;

import com.android.zhhr.data.entity.User;
import com.android.zhhr.db.helper.DaoHelper;

/**
 * Created by JasonChen on 2018/10/29.
 */
public class LoginManager {
    private boolean isLogin;
    private static LoginManager ourInstance;
    private DaoHelper mDaoHelper;
    private String currUserName;

    public static LoginManager getInstance(Context context) {
        if (ourInstance == null) {
            synchronized (LoginManager.class) {
                if (ourInstance == null) {
                    ourInstance = new LoginManager(context);
                }
            }
        }
        return ourInstance;
    }

    private LoginManager(Context context) {
        mDaoHelper = new DaoHelper(context);
    }

    @Nullable
    public String getCurrUserName() {
        return isLogin ? currUserName : null;
    }

    public boolean isLogin() {
        return isLogin;
    }

    public boolean signIn(String userName, String passWord) {
        boolean b = mDaoHelper.signUser(userName, passWord);
        isLogin = b;
        currUserName = userName;
        return b;
    }

    public User login(String userName, String passWord) {
        User user = mDaoHelper.Login(userName, passWord);
        isLogin = user != null;
        currUserName = userName;
        return user;
    }
}
